﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;
using System.Windows.Forms;
using System.Xml;
namespace Helper
{
    public class FileHelper
    {


      

        /// <summary>
        /// Get connection string
        /// </summary>
        public static string ConnectionString
        {
            get { return GetConnectString(); }
        }

       public static void SaveConnectString(string data)
        {
            
            
                var file =Application.StartupPath+"\\config.cf";
                
                if(File.Exists(file))
                {
                     File.Delete(Application.StartupPath+"\\config.cf");
                   
                }
                File.AppendAllText(file,Security.EncryptString(data));
            

        }

        static string GetConnectString()
        {
            try
            {
                var file = File.OpenText(Application.StartupPath+"\\config.cf");
                string cn =Security.DecryptString(file.ReadLine());
                file.Dispose();
                if (cn != null && cn.Length != 0)
                    return cn;
                return null;
            }
            catch
            {
                return null;
            }
        }

       public static string GetValueFromXml(string xmlfile, string tag)
        {
            try
            {
                XmlDocument xml = new XmlDocument();
                xml.Load(xmlfile);
                XmlNodeList nodes = xml.ChildNodes;
                string data = xml.SelectSingleNode(tag).InnerText;
               
                return data;
            }
            catch
            {
                return null;
            }
       }

       public static void SetValueForXml(string xmlfile, string tag, string value)
       {

           XmlDocument xml = new XmlDocument();
           xml.Load(xmlfile);
           XmlNodeList nodes = xml.ChildNodes;
           XmlNode node = xml.SelectSingleNode(tag);
           node.InnerText = value;
           xml.Save(xmlfile);
               
       }
    }
}
